/* Copyright(C): Leaf Johnson 2009,2010

    This file is part of leaf-lib.

    leaf-lib is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    leaf-lib is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with leaf-lib.  If not, see <http://www.gnu.org/licenses/>.


 * 
 * 
 */


#ifndef BASE_H_INCLUDED
#define BASE_H_INCLUDED 1

#include "types.h"
/*类型转换*/
#define PARSE_BOOL(val)         ( (val) ? True : False )
#define VOID_PTR(p, type)  		( (type *)(p) )
#define VOID_DATA(p, type)     	(* VOID_PTR(p, type))

/*位操作*/
#include <avr/sfr_defs.h>
#define mask_set( val, mask )      (val) |=  (mask)
#define mask_clr( val, mask )      (val) &= ~(mask)
#define  set_bit(P,B)               P |= _BV(B)
#define  clr_bit(P,B)               P &= ~_BV(B)
#ifndef sbi
#define  sbio( p, b )				set_bit( p, b )
#define  cbio( p, b )				clr_bit( p, b )
#endif
static inline bool test_bit( uint8_t reg, uint8_t bit )
{
	return PARSE_BOOL( reg & _BV(bit) );
}
#define bit_test( r, b ) ( (r) & _BV((b)) )

#define nop() 	__asm__ volatile ("nop" ::)

/* 数学运算 */
#define  ABS(val)               (((val) < 0) ? (-(val)) : (val))
static inline int absint( int val ){ return ABS(val); }
static long inline power( int base, int pow )
{
	if( pow == 0 ) return 1;
	long ret = base;
	while( --pow > 0 ){
		ret *= base;
	}
	return ret;
}

#define DEBUG_CODE(debug_frag) {       \
	debug_frag                         \
}

#endif
